import json
from datetime import datetime

from sqlalchemy import Integer, Column, String
from sqlalchemy.orm import declarative_base
from common.db_util import MySqlDb

# 创建基类
Base = declarative_base()

class Invoice(Base):
    __tablename__ = 'invoice'
    #字段如下：
    '''
        id
        invoice_type
        invoice_code
        issue_date
        buyer_name
        buyer_tax_id
        buyer_address_phone
        buyer_bank_account
        seller_name
        seller_tax_id
        seller_address_phone
        seller_bank_account
        check_code
        machine_code
        total_amount
        total_tax
        total_price_tax
        drawer
        created_at
        updated_at 
    '''
    id = Column(Integer, primary_key=True)
    invoice_type = Column(String(50))
    invoice_code = Column(String(50))
    issue_date = Column(String(50))
    buyer_name = Column(String(255))
    buyer_tax_id = Column(String(50))
    buyer_address_phone = Column(String(255))
    buyer_bank_account = Column(String(255))
    seller_name = Column(String(255))
    seller_tax_id = Column(String(50))
    seller_address_phone = Column(String(255))
    seller_bank_account = Column(String(255))
    check_code = Column(String(100))
    machine_code = Column(String(50))
    total_amount = Column(String(50))
    total_tax = Column(String(50))
    total_price_tax = Column(String(50))
    drawer = Column(String(50))
    created_at = Column(String(50))
    updated_at = Column(String(50))



class InvoiceItem(Base):
    __tablename__ = 'invoice_item'
    #下面是数据库的字段
    '''
        id
        invoice_id
        item_name
        specification
        unit
        quantity
        unit_price
        amount
        tax_rate
        tax_amount
        created_at
        updated_at
    '''
    id = Column(Integer, primary_key=True)
    invoice_id = Column(Integer)
    item_name = Column(String(255))
    specification = Column(String(255))
    unit = Column(String(50))
    quantity = Column(Integer)
    unit_price = Column(String(50))
    amount = Column(String(50))
    tax_rate = Column(String(50))
    tax_amount = Column(String(50))
    created_at = Column(String(50))
    updated_at = Column(String(50))


def createInvoiceItem(item):
   #保存发票项目
   session = MySqlDb.get_mysql_session()
   invoice_item = None
   try:
        invoice_item = InvoiceItem(**item)
   except Exception as e:
        print("插入发票项目失败：",e)
   #填充时间
   if invoice_item:
       invoice_item.created_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
       invoice_item.updated_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
       session.add(invoice_item)
       session.commit()
       print("插入发票项目成功：invoice_item id is:",invoice_item.id)
       return invoice_item.id

def createInvoice(invoice_dict:dict):
        session = MySqlDb.get_mysql_session()
        items =  invoice_dict.get("items")
        #删除items
        if "items" in invoice_dict:
            del invoice_dict["items"]
        invoice = Invoice(**invoice_dict)
        #填充时间
        invoice.created_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        invoice.updated_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        #存储发票
        session.add(invoice)
        #提交
        session.commit()
        print("插入发票成功：invoice id is:",invoice.id)
        #存储发票项目
        for item in items:
            item['invoice_id'] = invoice.id
            createInvoiceItem(item)
        return invoice.id
